global data /projects/programs/monarch/china/china/Q0-Data/use
global china ../china/data
global quality ../china/data/quality/
global suppdata ../china/suppdata/
global disclosure ../china/disclosure

***********************************************
*** Set up the baseline data and variables ****
***********************************************

forv x=2002/2008 {
use $china/imp`x'china, clear

* Drop related party and 0 quantity or value
drop if rel==1
drop if q==0
drop if v==0

* Price
gen p=v/q
gen log_price=log(p)

* Quality
merge m:1 hs manuf_id year using $quality/quality_est_final, keepusing(lambda)
drop if _m==2
drop _m

* Wholesale/retail indicator
merge m:1 firmid using $china/wr
drop if _m==2
replace firmid_wr=0 if firmid_wr==.
drop _m

* Within-HS standardized price
cap drop sd
cap drop mn
cap drop standard
cap drop standard_cat
bysort hs: egen sd=sd(log_price)
bysort hs: egen mn=mean(log_price)
gen standard=(log_price-mn)/sd
xtile standard_cat = standard, nquantiles(10)

* Importer and Exporter Size
bysort hs firmid year: egen imp_v=total(v)
bysort hs manuf_id year: egen double exp_v=total(v)
gen log_exp_v=log(exp_v)
gen log_imp_v=log(imp_v)

* Relationship size
bysort firmid manuf_id year: egen rel_v=total(v)
gen log_rel_v=log(rel_v)

/* Create tags for "How many x's per y?" */
egen exporter_importer_hs_tag=tag(hs firmid manuf_id)
egen exporter_importer_tag=tag(firmid manuf_id)
egen exporter_hs_tag=tag(hs manuf_id)
egen importer_hs_tag=tag(hs firmid)
egen exporter_tag=tag(manuf_id)
egen importer_tag=tag(firmid)
egen hs_tag=tag(hs)

* Number of Products per Relationship
bysort firmid manuf_id: egen rel_prods=total(exporter_importer_hs_tag)

* Number of Products per Supplier
bysort manuf_id: egen products_per_exporter=total(exporter_hs_tag)

* Number of Products per Buyer
bysort firmid: egen products_per_importer=total(importer_hs_tag)

* Number of Buyers per Supplier (within an HS)
bysort hs manuf_id: egen exporter_partners_inhs=total(exporter_importer_hs_tag)

* Number of Buyers per Supplier (total)
bysort manuf_id: egen exporter_partners=total(exporter_importer_tag)

* Number of Suppliers per Buyer (within an HS)
bysort hs firmid: egen importer_partners_inhs=total(exporter_importer_hs_tag)

* Number of Suppliers per Buyer (total)
bysort firmid: egen importer_partners=total(exporter_importer_tag)

* Number of Suppliers per Product
bysort hs: egen exporters_per_product=total(exporter_hs_tag)

* Number of Buyers per Product
bysort hs: egen importers_per_product=total(importer_hs_tag)

save $china/imp`x'china_baseline, replace
}

*********************************************************************************************
** Generate staying statistics for "StayVal"- did an importer stay with its "main supplier" **
*********************************************************************************************

forv x=2002/2007 {

use $china/imp`x'china_baseline, clear

local y=`x'+ 1

append using $china/imp`y'china_baseline
destring year, replace 

egen minyear=min(year)
egen maxyear=max(year)

bys hs firmid: egen firm_maxyear=max(year)

order hs firmid manuf_id year

gen individual_v_share=v/imp_v
bysort hs firmid year: egen max_ind_v=max(individual_v_share)
gen max_v=(max_ind_v==individual_v_share)
drop if max_v!=1
sort hs firmid year manuf_id
by hs firmid year: gen ct=_n
drop if ct>1

egen xt=group(manuf_id)
sort hs firmid year
by hs firmid: gen xt1=xt[_n+1]

gen stayed=(xt==xt1)
replace stayed=. if year!=minyear
replace stayed=. if firm_maxyear==minyear

gen city=substr(manuf_id,-3,3)
egen yt=group(city)
sort hs firmid year
by hs firmid: gen yt1=yt[_n+1]

gen city_stayed=(yt==yt1)
replace city_stayed=. if year!=minyear
replace city_stayed=. if firmid!=firmid[_n+1]

save $china/StayValSample`y', replace
}


*********************************************************************************************
** Generate staying statistics for "Stay"- did an importer stay with any particular supplier **
*********************************************************************************************
forv x=2002/2007 {

use $china/imp`x'china_baseline, clear

local y=`x'+ 1

append using $china/imp`y'china_baseline
destring year, replace 

egen minyear=min(year)
egen maxyear=max(year)

*bys hs firmid: egen firm_minyear=min(year)
bys hs firmid: egen firm_maxyear=max(year)
*drop if firm_minyear==firm_maxyear

order hs firmid manuf_id year

egen xt=group(manuf_id)
sort hs firmid manuf_id year
by hs firmid manuf_id: gen xt1=xt[_n+1]

gen stayed=(xt==xt1)
replace stayed=. if year!=minyear
replace stayed=. if firm_maxyear==minyear



save $china/StaySample`y', replace
}

*************************************************
* Summarize key moments of the baseline data ****
*************************************************

use $china/imp2002china_baseline, clear
forv x=2003/2008 {
append using $china/imp`x'china_baseline
}

************************
* Table 1 Panel A 
*************************
preserve
keep if firmid_wr==0
* Number of Products per Relationship
summ rel_prods if exporter_importer_tag==1, d
* Number of Products per Supplier
summ products_per_exporter if exporter_tag==1, d
* Number of Products per Buyer
summ products_per_importer if importer_tag==1, d
* Number of Buyers per Supplier (within an HS)
summ exporter_partners_inhs if exporter_hs_tag==1, d
* Number of Buyers per Supplier (total)
summ exporter_partners if exporter_tag==1, d
* Number of Suppliers per Buyer (within an HS)
summ importer_partners_inhs if importer_hs_tag==1, d
* Number of Suppliers per Buyer (total)
summ importer_partners if importer_tag==1, d
* Number of Suppliers per Product
summ exporters_per_product if hs_tag==1, d
* Number of Buyers per Product
summ importers_per_product if hs_tag==1, d
restore



************************
* Online Appendix Table A1 
*************************

preserve
* Number of Products per Relationship
summ rel_prods if exporter_importer_tag==1, d
* Number of Products per Supplier
summ products_per_exporter if exporter_tag==1, d
* Number of Products per Buyer
summ products_per_importer if importer_tag==1, d
* Number of Buyers per Supplier (within an HS)
summ exporter_partners_inhs if exporter_hs_tag==1, d
* Number of Buyers per Supplier (total)
summ exporter_partners if exporter_tag==1, d
* Number of Suppliers per Buyer (within an HS)
summ importer_partners_inhs if importer_hs_tag==1, d
* Number of Suppliers per Buyer (total)
summ importer_partners if importer_tag==1, d
* Number of Suppliers per Product
summ exporters_per_product if hs_tag==1, d
* Number of Buyers per Product
summ importers_per_product if hs_tag==1, d
restore


*********************************
** Table 1 Panel B: Distribution of Imports by Partner **
**********************************

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

keep if year==minyear
drop if firmid_wr==1

bysort hs firmid year: egen buyer_imports=total(v)
bysort hs firmid year: egen rank_v=rank(v), field
gen exporter_share=v/buyer_imports
summ exporter_share if rank_v==1, d
summ exporter_share if rank_v==1 & importer_partners_inhs>1, d
summ exporter_share if rank_v==2 , d
summ exporter_share if rank_v==2 & importer_partners_inhs>2, d


*********************************
** Table 2: Margin Regressions **
**********************************

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

drop if firmid_wr==1

gen year_is_minyear=(year==minyear)
bysort year hs: egen total_trade=total(v*year_is_minyear)
bysort year hs: egen total_suppliers=total(exporter_tag*year_is_minyear)
bysort year hs: egen total_buyers=total(importer_tag*year_is_minyear)
bysort year hs: egen nonzero_combinations=total(year_is_minyear)
gen total_combinations=total_suppliers*total_buyers
gen density=nonzero_combinations/total_combinations
gen mean_trade=total_trade/nonzero_combinations
gen log_total_trade=log(total_trade)
gen log_total_suppliers=log(total_suppliers)
gen log_total_buyers=log(total_buyers)
gen log_density=log(density)
gen log_mean_trade=log(mean_trade)
egen year_hs_tag2=tag(year hs)
gen year_hs_tag=(year_hs_tag2==1 & log_total_suppliers!=. & log_total_buyers!=. & log_density!=. & log_mean_trade!=.)
reg log_total_suppliers log_total_trade if year_hs_tag==1, noconstant
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Margin Regression within HS") dec(4)
reg log_total_buyers log_total_trade if year_hs_tag==1, noconstant
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Margin Regression within HS") dec(4)
reg log_density log_total_trade if year_hs_tag==1, noconstant
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Margin Regression within HS") dec(4)
reg log_mean_trade log_total_trade if year_hs_tag==1, noconstant
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Margin Regression within HS") dec(4)

gen mean_trade_buyeronly=total_trade/total_buyers
gen log_mean_trade_buyeronly=log(mean_trade_buyeronly)
reg log_total_buyers log_total_trade if year_hs_tag==1, noconstant
reg log_mean_trade_buyeronly log_total_trade if year_hs_tag==1, noconstant
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Margin Regression within HS") dec(4)



************************************************
***** Main Regressions **********************************
************************************************



*************************
** "Stay" Regressions ***
*************************
forv x=2003/2008 {
! gunzip $china/StaySample`x'.dta.gz
}


use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

/* Share of Importers staying with their partner */
summ stayed



char standard_cat[omit] 5

_pctile lambda, p(1 99)
local lambda_high=r(r2)
local lambda_low=r(r1)
drop if lambda>`lambda_high'
drop if lambda<`lambda_low'

destring hs, gen(hs_num)

*****************************************************
*Table 4 Panel A (Wholesaler/Retailers Excluded) ****
*****************************************************
** Column 1
preserve
keep if firmid_wr==0
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4) replace
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)
** Column 2
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)
** Column 3
xi: areg stayed log_price  	lambda 			i.year 				rel_prods, 	absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price  	lambda 			i.year 				rel_prods, 	absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)
** Column 4
xi: areg stayed log_price  	lambda 			i.year log_exp_v log_imp_v 	rel_prods, 	absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price  	lambda 			i.year log_exp_v log_imp_v 	rel_prods, 	absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)

restore

*****************************************************
*Table 4 Panel B (All US Firms) *********************
*****************************************************

** Column 1
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE All Firms Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE All Firms Bootstrap Cluster HS") dec(4)
** Column 2
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE All Firms Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE All Firms Bootstrap Cluster HS") dec(4)



keep if firmid_wr==0

preserve
**************************************************
*Table 5 Panel A (Importer-Exporter Level) *******
***************************************************
*Gemerate within-relationship hs weights
replace v=. if year!=minyear
bysort hs firmid manuf_id year: egen rel_hs_v=total(v)
gen rel_hs_share=rel_hs_v/rel_v
* Weight the standard price (for comparability across products)
gen standard_price_weights=rel_hs_share*standard
bysort firmid manuf_id year: egen weighted_standard_price=total(standard_price_weights)
* Weight quality
gen lambda_weights=rel_hs_share*lambda
bysort firmid manuf_id year: egen weighted_lambda=total(lambda_weights)
replace weighted_lambda=. if lambda==.
* Weight the staying decision (rounding)
gen stayed_weights=rel_hs_share*stayed
bysort firmid manuf_id year: egen weighted_stay=total(stayed_weights)
replace weighted_stay=round(weighted_stay)
** Generate dummies variables for stayed with any or stayed with all.
bysort firmid manuf_id year: egen stayed_any=max(stayed)
bysort firmid manuf_id year: egen stayed_all=min(stayed)
* Figure out the main product of that relationship
bysort firmid manuf_id year: egen max_hs_share=max(rel_hs_share)
gen rel_hs_num=0
replace rel_hs_num=hs_num if max_hs_share==rel_hs_share
bysort firmid manuf_id year: egen max_rel_hs_num=max(rel_hs_num)
replace rel_hs_num=max_rel_hs_num
replace weighted_standard_price=. if year==2008

** Column 1 Weighted Average of Staying Decision across Products
xi: areg weighted_stay weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg weighted_stay weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)

** Column 2 Weighted Average of Staying Decision across Products
xi: areg weighted_stay weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg weighted_stay weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)

** Column 3 Stayed with Any Partner
xi: areg stayed_any weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg stayed_any weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)

** Column 4 Stayed with Any Partner
xi: areg stayed_any weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg stayed_any weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)

** Column 5 Stayed with All Partner
xi: areg stayed_all weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg stayed_all weighted_standard_price  weighted_lambda i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)

** Column 6 Stayed with All Partners
xi: areg stayed_all weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Cluster Importer") dec(4)
xi: areg stayed_all weighted_standard_price  weighted_lambda log_exp_v log_imp_v i.year if exporter_importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Weights HS Size in RelYear HS FE Bootstrap Cluster HS") dec(4)
restore

*******************************************
*Table 5 Panel B (Single Supplier) ********
*********************************************
preserve
keep if importer_partners_inhs==1
** Column 1 One Supplier Within-HS
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 HS Supplier Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 HS Supplier Bootstrap Cluster HS") dec(4)

** Column 2 One Supplier Within-HS
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 HS Supplier Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 HS Supplier Bootstrap Cluster HS") dec(4)
restore

** Only a single partner (but potentially many products from that partner)
preserve
keep if importer_partners==1
*Gemerate within-relationship hs weights
replace v=. if year!=minyear
bysort hs firmid manuf_id year: egen rel_hs_v=total(v)
gen rel_hs_share=rel_hs_v/rel_v
* Weight the standard price (for comparability across products)
gen standard_price_weights=rel_hs_share*standard
bysort firmid manuf_id year: egen weighted_standard_price=total(standard_price_weights)
* Weight quality
gen lambda_weights=rel_hs_share*lambda
bysort firmid manuf_id year: egen weighted_lambda=total(lambda_weights)
replace weighted_lambda=. if lambda==.
* Weight the staying decision (rounding)
gen stayed_weights=rel_hs_share*stayed
bysort firmid manuf_id year: egen weighted_stay=total(stayed_weights)
replace weighted_stay=round(weighted_stay)
** Can also do stayed, meaning stayed with any or stayed with all.
bysort firmid manuf_id year: egen stayed_any=max(stayed)
bysort firmid manuf_id year: egen stayed_all=min(stayed)
* Figure out the main product of that relationship
bysort firmid manuf_id year: egen max_hs_share=max(rel_hs_share)
gen rel_hs_num=0
replace rel_hs_num=hs_num if max_hs_share==rel_hs_share
bysort firmid manuf_id year: egen max_rel_hs_num=max(rel_hs_num)
replace rel_hs_num=max_rel_hs_num

** Column 3 One Supplier Total
xi: areg weighted_stay weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Cluster Importer") dec(4)
xi: areg weighted_stay weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Bootstrap Cluster HS") dec(4)

** Column 4 One Supplier Total
xi: areg stayed_any weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Cluster Importer") dec(4)
xi: areg stayed_any weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Bootstrap Cluster HS") dec(4)

** Column 5 One Supplier Total
xi: areg stayed_all weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(cluster firmid)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Cluster Importer") dec(4)
xi: areg stayed_all weighted_standard_price  weighted_lambda i.year if importer_tag==1 & year==minyear, ///
absorb(rel_hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE 1 Supplier Bootstrap Cluster HS") dec(4)
restore

***********************************************
*Table 5 Panel C Multi-Year Relationships *****
***********************************************

keep if year==minyear
sort hs manuf_id firmid year
by hs manuf_id firmid: gen rel_age=_n
*Eliminate all 1-year relationships
preserve
keep if rel_age>1

** Column 1 No Single-Year Relationships
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, No 1year Rels Year and HS FE Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, No 1year Rels Year and HS FE Bootstrap Cluster HS") dec(4)

** Column 2 No Single-Year Relationships
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, No 1year Rels Year and HS FE Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, No 1year Rels Year and HS FE Bootstrap Cluster HS") dec(4)
restore

*Different windows of time: 2, 5 years
sort hs firmid manuf_id year
by  hs firmid manuf_id: gen manuf_id2=manuf_id[_n+2]
gen stayed_2yrs=(manuf_id==manuf_id2)
by  hs firmid manuf_id: gen manuf_id5=manuf_id[_n+5]
gen stayed_5yrs=(manuf_id==manuf_id5)

preserve
keep if year==2002 | year==2003 | year==2004 | year==2005

**Column 3 "Stay" Defined over 2 years, not 1
xi: areg stayed_2yrs log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Cluster HS") dec(4)
xi: areg stayed_2yrs log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Bootstrap Cluster HS") dec(4)
** Column 4 "Stay" Defined over 2 years, not 1
xi: areg stayed_2yrs log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Cluster HS") dec(4)
xi: areg stayed_2yrs log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Bootstrap Cluster HS") dec(4)
restore

preserve
keep if year==2002

**Column 5 "Stay" Defined over 5 years, not 1
xi: areg stayed_5yrs log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Cluster HS") dec(4)
xi: areg stayed_5yrs log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Bootstrap Cluster HS") dec(4)

** Column 6 "Stay" Defined over 5 years, not 1
xi: areg stayed_5yrs log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Cluster HS") dec(4)
xi: areg stayed_5yrs log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year HS FE Bootstrap Cluster HS") dec(4)
restore


*************************
** "StayVal" Regressions ***
*************************

forv x=2003/2008 {
! gunzip $china/StayValSample`x'.dta.gz
}

use $china/StayValSample2003, clear
append using $china/StayValSample2004
append using $china/StayValSample2005
append using $china/StayValSample2006
append using $china/StayValSample2007
append using $china/StayValSample2008

/* Share of importers staying with their supplier */
summ stayed
/* Share of importers staying in their original supplier's city */
summ city_stayed

_pctile lambda, p(1 99)
local lambda_high=r(r2)
local lambda_low=r(r1)
drop if lambda>`lambda_high'
drop if lambda<`lambda_low'

destring hs, gen(hs_num)

********************************************************
*Table 4 Panel A (Wholesaler/Retailers Excluded) ******
********************************************************

** Column 6
preserve
keep if firmid_wr==0
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4) 
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)

** Column 6
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)

** Column 7
xi: areg stayed log_price  	lambda 			i.year 				rel_prods, 	absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price  	lambda 			i.year 				rel_prods, 	absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)

** Column 8
xi: areg stayed log_price  	lambda 			i.year log_exp_v log_imp_v 	rel_prods, 	absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Cluster HS") dec(4)
xi: areg stayed log_price  	lambda 			i.year log_exp_v log_imp_v 	rel_prods, 	absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE No W/R Bootstrap Cluster HS") dec(4)
restore

*******************************************
*Table 4 Panel B (All US Firms) ************
*******************************************

** Column 3
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year, 					absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE Bootstrap Cluster HS") dec(4)

** Column 4
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs) vce(cluster hs)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE Cluster HS") dec(4)
xi: areg stayed log_price 	lambda 			i.year log_exp_v log_imp_v, 			absorb(hs_num) vce(bootstrap, cluster(hs))
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year and HS FE Bootstrap Cluster HS") dec(4)



***************
** Figure 1 ***
****************
***********************
** Estimating correlation of staying share (at HS2 level) with Industry Characteristics
************************

* 
***************************************
* Generate Nunn Measure at HS2 level **
***************************************

/* Generate total trade in an HS in 1997 */
use $suppdata/supplier1997, clear
collapse (sum) v, by(hs)
save $china/hs1997, replace
/* Open HS- IO Code concordance */ 
use $suppdata/HSConcord, clear
drop IO_des hs_des
gen hs_length=length(hs)
replace hs="0"+hs if hs_length==9
drop hs_length
/* Merge concordance with 1997 trade values */ 

merge m:1 hs using $china/hs1997
keep if _m==3
drop _m
ren IOCode industry_code

/* Merge on Nunn measures of contract intensity */
merge m:1 industry_code using $suppdate/contract_intensity_IO_1997, keepusing(frac_lib_diff frac_lib_not_homog)
keep if _m==3
drop _m
duplicates tag hs, gen(dup)
drop if dup!=0
drop dup
gen hs2=substr(hs,1,2)
ren v hs_v
/* To aggregate up to HS2 level, determine the import weight of each IO Code in each HS2. */
bysort hs2 industry_code: egen within_hs2_io_v=total(hs_v)
gen io_weight_in_hs2=within_hs2_io_v/hs2_v
duplicates drop hs2 industry_code, force
drop hs
/* Generate HS2 versions of the Nunn measure, weighted by the import weight of each IO Code */
gen weighted_z1=frac_lib_diff*io_weight_in_hs2
by hs2: egen hs2_z1=total(weighted_z1)
gen weighted_z2=frac_lib_not_homog*io_weight_in_hs2
by hs2: egen hs2_z2=total(weighted_z2)
duplicates drop hs2, force
keep hs2 hs2_z1 hs2_z2
save $china/hs2_contract_intensity, replace

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

drop if firmid_wr==1
drop if year!=minyear
gen hs2=substr(hs,1,2)
merge m:1 hs2 using $china/hs2_contract_intensity
drop if _m!=3
drop _m
/* Simple average of "stayed" */
bysort hs2 year: egen mean_stayed=mean(stayed)
bysort hs2 year: egen hs2_v=total(v)
gen rel_share_in_hs2=v/hs2_v
gen rel_weight=rel_share_in_hs2*stayed
/* Stayed, weighted by size of the trade relationship in that HS2 */
bysort hs2 year: egen rel_weighted_hs2_stay=total(rel_weight)
egen hs2_year_tag=tag(hs2 year)

/* Regressions of contract intensity on staying share (not disclosed) */
areg mean_stayed hs2_z1 if hs2_year_tag==1, abs(year)
areg rel_weighted_hs2_stay hs2_z1 if hs2_year_tag==1, abs(year)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year FE") dec(4)
areg mean_stayed hs2_z2 if hs2_year_tag==1, abs(year)
areg rel_weighted_hs2_stay hs2_z2 if hs2_year_tag==1, abs(year)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year FE") dec(4)

keep hs2 mean_stayed rel_weighted_hs2_stay hs2_z1 hs2_z2 hs2_year_tag year
keep if hs2_year_tag==1 & year==2004
save $china/plots_nunn_2004, replace


***************************************
* Generate BW Elasticity Measure at HS2 level **
* Not disclosed 
***************************************


use $suppdata/bw_elast, clear
merge 1:1 hs using $suppdata/hs1997
drop if _m!=3
drop _m
gen hs2=substr(hs,1,2)
bysort hs2: egen hs2_v=total(v)
gen hs_share_in_hs2=v/hs2_v
gen hs_weight_in_hs2=sigma*hs_share_in_hs2
bysort hs2: egen hs_weighted_hs2_sigma=total(hs_weight_in_hs2)
keep hs2 hs_weighted_hs2_sigma
duplicates drop hs2, force
save $suppdata/hs2_bw_sigma, replace

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

drop if firmid_wr==1
drop if year!=minyear
gen hs2=substr(hs,1,2)
merge m:1 hs2 using $china/hs2_bw_sigma
bysort hs2 year: egen mean_stayed=mean(stayed)
bysort hs2 year: egen hs2_v=total(v)
gen rel_share_in_hs2=v/hs2_v
gen rel_weight=rel_share_in_hs2*stayed
bysort hs2 year: egen rel_weighted_hs2_stay=total(rel_weight)

egen hs2_tag=tag(hs2)
egen hs2_year_tag=tag(hs2 year)
reg rel_weighted_hs2_stay hs_weighted_hs2_sigma i.year if hs2_year_tag==1 & year==2002
reg mean_stayed hs_weighted_hs2_sigma i.year if hs2_year_tag==1 & year==2002
twoway (scatter rel_weighted_hs2_stay hs_weighted_hs2_sigma if hs2_year_tag==1 & hs_weighted_hs2_sigma<20 & year==2002) ///
(lfit rel_weighted_hs2_stay hs_weighted_hs2_sigma if hs2_year_tag==1 & hs_weighted_hs2_sigma<20 & year==2002)
twoway (scatter mean_stayed hs_weighted_hs2_sigma if hs2_year_tag==1 & hs_weighted_hs2_sigma<20 & year==2002) ///
 (lfit mean_stayed hs_weighted_hs2_sigma if hs2_year_tag==1 & hs_weighted_hs2_sigma<20 & year==2002)

******************************************************
* Generate Capital and Skill Intensity at HS2 level **
******************************************************

use $suppdata/naics5809, clear
drop if year<2002
drop if year>2007
gen capital=cap*1000000
gen employment=emp*1000
gen nonprod_employment=emp*1000 - prode*1000
gen cap_intensity=log(capital/employment)
gen skill_intensity=log(nonprod_employment/employment)
collapse (mean) cap_intensity skill_intensity, by(naics)
save $china/naics_intensities, replace

use $china/hs_naics7_imports_07, clear
gen hs_length=length(hs)
replace hs="0"+hs if hs_length==9
drop hs_length
keep hs naicsbaseroot
ren naicsbaseroot naics
destring naics, replace force
merge m:1 naics using $china/naics_intensities
drop if _m!=3
drop _m
merge m:1 hs using $china/hs2007
drop if _m!=3
drop _m
gen hs2=substr(hs,1,2)
ren v hs_v
bysort hs2: egen hs2_v=total(hs_v)
gen hs_weight_in_hs2=hs_v/hs2_v
bysort hs2 naics: egen within_hs2_naics_v=total(hs_v)
gen naics_weight_in_hs2=within_hs2_naics_v/hs2_v
duplicates drop hs2 naics, force
drop hs
gen weighted_cap_intensity=cap_intensity*naics_weight_in_hs2
by hs2: egen hs2_cap_intensity=total(weighted_cap_intensity)
gen weighted_skill_intensity=skill_intensity*naics_weight_in_hs2
by hs2: egen hs2_skill_intensity=total(weighted_skill_intensity)
duplicates drop hs2, force
keep hs2 hs2_cap_intensity hs2_skill_intensity
save $china/hs2_intensities, replace

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

drop if year!=minyear
drop if firmid_wr==1

gen hs2=substr(hs,1,2)
merge m:1 hs2 using $china/hs2_intensities
drop if _m!=3
drop _m
bysort hs2 year: egen mean_stayed=mean(stayed)
bysort hs2 year: egen hs2_v=total(v)
gen rel_share_in_hs2=v/hs2_v
gen rel_weight=rel_share_in_hs2*stayed
bysort hs2 year: egen rel_weighted_hs2_stay=total(rel_weight)
egen hs2_year_tag=tag(hs2 year)

/* Regressions of skill/capital intensity on staying share (not disclosed) */
areg rel_weighted_hs2_stay hs2_cap_intensity if hs2_year_tag==1, abs(year)
areg rel_weighted_hs2_stay hs2_skill_intensity if hs2_year_tag==1, abs(year)
areg mean_stayed hs2_cap_intensity if hs2_year_tag==1, abs(year)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year FE") dec(4)
areg mean_stayed hs2_skill_intensity if hs2_year_tag==1, abs(year)
outreg2 using $disclosure/regs.txt,  addtext("Stay Sample, Year FE") dec(4)



keep hs2 mean_stayed rel_weighted_hs2_stay hs2_cap_intensity hs2_skill_intensity hs2_year_tag year
keep if hs2_year_tag==1 & year==2004
save $china/plots_intensity_2004, replace

*************
* Make data underlying Figure 1 
************


use $china/plots_intensity_2004, clear
merge 1:1 hs2 using $china/plots_nunn_2004
drop if _m!=3
drop hs2_year_tag _m

order hs2 mean_stayed rel_weighted_hs2_stay hs2_z1 hs2_z2 hs2_cap_intensity hs2_skill_intensity
replace mean_stayed=round(mean_stayed,0.0001)
replace rel_weighted_hs2_stay=round(rel_weighted_hs2_stay,0.0001)
replace hs2_z1=round(hs2_z1,0.0001)
replace hs2_z2=round(hs2_z2,0.0001)
replace hs2_cap_intensity=round(hs2_cap_intensity,0.0001)
replace hs2_skill_intensity=round(hs2_skill_intensity,0.0001)
outfile using $disclosure/stay_share_plots.xls, comma replace



*************************************
** Other statistics in the paper ***
**************************************


* How often does the same supplier charge different prices for the same product to different consumers

use $china/StaySample2003, clear
append using $china/StaySample2004
append using $china/StaySample2005
append using $china/StaySample2006
append using $china/StaySample2007
append using $china/StaySample2008

keep if year==minyear
drop if firmid_wr==1
bysort hs manuf_id year: egen mean_exporter_price=mean(p)
bysort hs manuf_id year: egen med_exporter_price=median(p)
tab exporter_partners_inhs if exporter_hs_tag==1

gen med_exporter_price_110t=1.10*med_exporter_price
gen med_exporter_price_90t=0.9*med_exporter_price
gen exporter_price_10p=(p<med_exporter_price_110t & p>med_exporter_price_90t)
tab exporter_price_10p if exporter_hs_tag==1
tab exporter_price_10p if exporter_hs_tag==1 & exporter_partners_inhs>1
tab exporter_price_10p if exporter_hs_tag==1 & exporter_partners_inhs==1

gen mean_exporter_price_110t=1.10*mean_exporter_price
gen mean_exporter_price_90t=0.9*mean_exporter_price
gen exporter_price_mean10p=(p<mean_exporter_price_110t & p>mean_exporter_price_90t)
tab exporter_price_mean10p if exporter_hs_tag==1
tab exporter_price_mean10p if exporter_hs_tag==1 & exporter_partners_inhs>1
tab exporter_price_mean10p if exporter_hs_tag==1 & exporter_partners_inhs==1

gen med_exporter_price_130t=1.30*med_exporter_price
gen med_exporter_price_70t=0.7*med_exporter_price
gen exporter_price_30p=(p<med_exporter_price_130t & p>med_exporter_price_70t)
tab exporter_price_30p if exporter_hs_tag==1
tab exporter_price_30p if exporter_hs_tag==1 & exporter_partners_inhs>1
tab exporter_price_30p if exporter_hs_tag==1 & exporter_partners_inhs==1

gen mean_exporter_price_130t=1.30*mean_exporter_price
gen mean_exporter_price_70t=0.7*mean_exporter_price
gen exporter_price_mean30p=(p<mean_exporter_price_130t & p>mean_exporter_price_70t)
tab exporter_price_mean30p if exporter_hs_tag==1
tab exporter_price_mean30p if exporter_hs_tag==1 & exporter_partners_inhs>1
tab exporter_price_mean30p if exporter_hs_tag==1 & exporter_partners_inhs==1

bys year hs: egen geo_log_price=mean(log_price)
replace geo_log_price=exp(geo_log_price)

bysort hs manuf_id year: egen max_price=max(p)
bysort hs manuf_id year: egen min_price=min(p)
bysort hs manuf_id year: egen av_price=mean(p)
gen price_diff_share=(max_price-min_price)/min_price
summ price_diff_share if exporter_hs_tag==1, d
summ price_diff_share if exporter_hs_tag==1 & exporter_partners_inhs>1, d
areg log_price log_rel_v, absorb(hs)
areg log_price rel_prods, absorb(hs)



*****************************************************************************
** Statistics on changes to the supply network and threshold switching **
****************************************************************************

/* Same Set of Partners */

use $china/StayValSample2006, clear

bysort hs firmid year: egen min_ps=min(stay)
egen hs_firmid_year_tag=tag(hs firmid year)
tab min_ps if year==2006 & hs_firmid_year_tag==1
/* Only XX% of importers have no change to their supplier network (XX% of importers alter their supplier network in some way) */

gen ss_v=min_ps*v
bysort year: egen same_set_val=total(ss_v)
bysort year: egen tot_trade=total(v)
gen share=same_set_val/tot_trade
tab share

/* Newly Added Partners */

sort hs hs_firmid_year_tag manuf_id year
by hs hs_firmid_year_tag: gen new_partner=(manuf_id!=manuf_id[_n-1] & year==2006)
bysort hs hs_firmid_year_tag year: egen max_np=max(new_partner)
tab max_np if year==2006 & hs_firmid_year_tag==1
bysort hs hs_firmid_year_tag year: egen total_added=total(new_partner)
summ total_added if hs_hs_firmid_year_tag_year_tag==1 & year==2006 & total_added>0

gen np_v=new_partner*v
bysort year: egen new_partner_val=total(np_v)
gen np_share=new_partner_val/tot_trade
tab np_share

/* Newly Dropped Partners */

sort hs firmid manuf_id year
by hs firmid: gen dropped_partner=(manuf_id!=manuf_id[_n+1] & year==2005)
bysort hs firmid year: egen max_dp=max(dropped_partner)
tab max_dp if year==2005 & hs_firmid_year_tag==1
bysort hs firmid year: egen total_dropped=total(dropped_partner)
summ total_dropped if hs_firmid_year_tag==1 & year==2005 & total_dropped>0

gen dp_v=dropped_partner*v
bysort year: egen dropped_partner_val=total(dp_v)
gen dp_share=dropped_partner_val/tot_trade
tab dp_share

bysort hs firmid: egen max_max_dp=max(max_dp)
gen no_change=(max_max_dp==0 & max_np==0)
gen add_partner=(max_max_dp==0 & max_np==1)
gen drop_partner=(max_max_dp==1 & max_np==0)
gen add_and_drop_partner=(max_max_dp==1 & max_np==1)



tab no_change if year==2006 & hs_firmid_year_tag==1
tab add_partner if year==2006 & hs_firmid_year_tag==1
tab drop_partner if year==2006 & hs_firmid_year_tag==1
tab add_and_drop_partner if year==2006 & hs_firmid_year_tag==1


gen nc_v=no_change*v
bysort year: egen no_change_val=total(nc_v)
gen nc_share=no_change_val/tot_trade

gen ap_v=add_partner*v
bysort year: egen add_partner_val=total(ap_v)
gen ap_share=add_partner_val/tot_trade

gen dp_v=drop_partner*v
bysort year: egen drop_partner_val=total(dp_v)
gen dp_share=drop_partner_val/tot_trade

gen adp_v=add_and_drop_partner*v
bysort year: egen add_and_drop_partner_val=total(adp_v)
gen adp_share=add_and_drop_partner_val/tot_trade

tab nc_share if year==2006
tab ap_share if year==2006
tab dp_share if year==2006
tab adp_share if year==2006


/* Do you switch often to a familiar partner? */

use $china/StayValSample2006, clear
append using $china/StayValSample2005

gen switched = (stay=0)
destring year, replace
replace year=year_num if year==.

cap drop minyear maxyear
egen minyear=min(year)
egen maxyear=max(year)

sort hs firmid manuf_id year 

gen familiar_switch=1 if switched==1 & manuf_id==manuf_id[_n-1] & firmid==firmid[_n-1]
replace familiar_switch=0 if switched==1 & manuf_id!=manuf_id[_n-1]
replace familiar_switch=0 if switched==1 & manuf_id==manuf_id[_n-1] & firmid!=firmid[_n-1]
replace familiar_switch=. if year==minyear

tab switch if year==maxyear
tab familiar_switch if year==maxyear


/* Threshold Switching */

use $china/StayValSample2006, clear


gen switched=(stayed==0)
tab switched if year==2005

keep if year==2005 & switched==1
tostring year, replace
append using $china/StayValSample2006
drop if city=="KOW" | city=="HON" |city=="GAO"
drop if province==""
bysort hs firmid year: egen tot_v_06=total(v)
gen individual_v_share_06=v/tot_v_06

sort hs firmid manuf_id year

replace individual_v_share_06=individual_v_share_06[_n+1] if switched==1 & manuf_id==manuf_id[_n+1] ///
	& firmid==firmid[_n+1]
replace individual_v_share_06=0 if switched==1 & manuf_id!=manuf_id[_n+1]
gen switched_diff=individual_v_share-individual_v_share_06

gen switched_diff_u10=(switched_diff<.10)
tab switched_diff_u10 if switched_diff!=.

gen switched_diff_u25=(switched_diff<.25)
tab switched_diff_u25 if switched_diff!=.

gen switched_diff_u40=(switched_diff<.4)
tab switched_diff_u40 if switched_diff!=.

/* Punch Line: X% of switches are occurring with changes of 25% or less */





